package com.robot.orders.controller;

import com.robot.orders.pojo.SettlementDetails;
import com.robot.orders.service.SettlementDetailsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * settlementDetails控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/settlementDetails")
public class SettlementDetailsController {
    @Autowired
    private SettlementDetailsService settlementDetailsService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.SUCCESS_QUERY, settlementDetailsService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.SUCCESS_QUERY, settlementDetailsService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<SettlementDetails> pageList = settlementDetailsService.findSearch(searchMap, page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<SettlementDetails>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.SUCCESS_QUERY, settlementDetailsService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param settlementDetails
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody SettlementDetails settlementDetails) {
        settlementDetailsService.add(settlementDetails);
        return new Result(StatusCode.SUCCESS_DELETED);
    }

    /**
     * 修改
     *
     * @param settlementDetails
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody SettlementDetails settlementDetails, @PathVariable String id) {
        settlementDetails.setId(id);
        settlementDetailsService.update(settlementDetails);
        return new Result(StatusCode.SUCCESS_UPDATED);
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        settlementDetailsService.deleteById(id);
        return new Result(StatusCode.SUCCESS_DELETED);
    }
}
